#!/usr/bin/env bash

alias_show() {
  \typeset expanded_alias_name

  if
    [[ -z "${1:-$alias_name}" ]]
  then
    rvm_log "usage: 'rvm alias show [alias_name]'"
    return 1
  fi

  [[ -s "$rvm_path/config/alias" ]] || return 2

  expanded_alias_name="$( __rvm_db_ "$rvm_path/config/alias" "${1:-$alias_name}")"

  if
    [[ -z "$expanded_alias_name" ]]
  then
    rvm_error "Unknown alias name: '${1:-$alias_name}'"
    return 3
  else
    if [[ -n "$gemset_name" ]]
    then printf "%b" "${expanded_alias_name}${rvm_gemset_separator:-"@"}${gemset_name}\n"
    else printf "%b" "${expanded_alias_name}\n"
    fi
  fi
}

alias_after_delete_default()
{
  [[ "$1" == "default" ]] || return 0

  for _path in "$rvm_bin_path"/default_*
  do
    # remove default_ruby
    if [[ -f "$_path" ]]
    then \command \rm -rfv "${_path}" || return $?
    fi
    # remove ruby
    _path="${_path#default_}"
    if [[ -f "$_path" ]]
    then \command \rm -rfv "${_path}" || return $?
    fi
  done

  for _path in "$rvm_path"/wrappers/default/* gem irb erb ri rdoc testrb rake ruby bundle
  do
    _path="${_path##*\/}"
    \command \rm -fv "$rvm_bin_path/${_path}" || return $?
  done
}

alias_delete_execute()
{
  for _path in "$rvm_rubies_path" "$rvm_gems_path" "$rvm_environments_path" "$rvm_wrappers_path"
  do
    if [[ -L "$_path/$1" ]]
    then \command \rm -fv "$_path/$1" || return $?
    fi
  done

  __rvm_db_ "$rvm_path/config/alias" "$1" "delete"

  alias_after_delete_default "$1" || return $?
}

alias_delete()
{
  __rvm_log_command "alias_delete" \
    "Deleting alias: ${1:-$alias_name}" \
    alias_delete_execute "${1:-$alias_name}" ||
    return $?
}
